import { InfoCrudMixin } from 'lin-mizar';
import { merge } from 'lodash';
import { Sequelize, Model } from 'sequelize';
import sequelize from '../lib/db';

class Task extends Model {
  toJSON() {
    const origin = {
      id: this.id,
      name: this.name,
      time: this.time,
      to: this.to,
      from: this.from,
      info: this.info,
    };
    return origin;
  }
}

Task.init(
  {
    id: {
      type: Sequelize.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    name: {
      type: Sequelize.STRING(50),
      allowNull: false
    },
    time: {
      type: Sequelize.STRING(30),
      allowNull: true,
      defaultValue: '未名'
    },
    to: {
      type: Sequelize.STRING(50),
      allowNull: false
    },
    from: {
      type: Sequelize.STRING(50),
      allowNull: false
    },
    info: {
      type: Sequelize.STRING(1000),
      allowNull: true
    },

  },
  merge(
    {
      sequelize,
      tableName: 'task',
      modelName: 'task'
    },
    InfoCrudMixin.options
  )
);

export { Task };
